<?php
/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.     *
 *                                                                    *
 **********************************************************************/

/**
 * Ageset entity manager.
 *
 * @version $Id: CommentsCommonManager.php,v 1.27 2008/08/21 08:44:16 max Exp $
 */

class CommentsCommonManager extends ORMManager  
{
	
	function getUserCokieNick($cookie_value, $ip)
	{

		$result = $this->getQuery()->select('nic_name')->from('NewsCommon_UserCommentsNicsDBO')->where("cookie='$cookie_value' and	ip=$ip")->limit(1)->fetchOne();
		if (is_object($result))
			return $result->nic_name;
		else 
			return false;	
	}
	
	function SetUserCokieNick($cookie_value, $ip, $nick)
	{
		$user_comments_nics = new NewsCommon_UserCommentsNicsDBO();
		$user_comments_nics->cookie = $cookie_value;
		$user_comments_nics->ip = $ip;
		$user_comments_nics->nic_name = $nick;
		$user_comments_nics->save();
		return $user_comments_nics->nic_id;
	}
	
	function selectCommentsByIp($comment_id)
	{
		$result = $this->getQuery()->from('NewsCommon_CommentsDBO')->where('comment_id='.$comment_id)->fetchOne()->toArray();
		
		$result = $this->getQuery()->select('comment_id, common_reiting')->from('NewsCommon_CommentsDBO')->where('ip='.$result['ip']." and bj_type_id=".$result['obj_type_id']." and obj_id=".$result['obj_id'])->fetchArray();
		return $this->getAssArray($result, 'comment_id', 'common_reiting');
	}
	
	function getCommentReiting($comment_id)
	{
		$result = $this->getQuery()->select('common_reiting')->from('NewsCommon_CommentsDBO')->where('comment_id='.$comment_id)->fetchOne()->toArray();
		return $result['common_reiting']*1;
	}
	
	function setCommentReiting($comment_id, $increment, $ip=0)
	{
		$tmp = $this->getQuery()->from('NewsCommon_CommentsDBO')->where("comment_id=$comment_id and ip=$ip")->fetchOne();
		
		$tmp_pull = $this->getQuery($qs)->from('NewsCommon_CommentPullIpDBO')->where("comment_id=$comment_id and ip=$ip")->fetchOne();
		
		$status = 0;
		
		if (!is_object($tmp) && !is_object($tmp_pull))
		{
			$status = 1;
			$this->getQuery()->update('NewsCommon_CommentsDBO')->set('reiting_click','reiting_click+1')->set('common_reiting','common_reiting+'.$increment)->where("comment_id=$comment_id 	and ip<>$ip")->execute();
		}
		
		$comment_pull = new NewsCommon_CommentPullIpDBO();
		$comment_pull->comment_id = $comment_id;
		$comment_pull->ip = $ip;
		$comment_pull->increment = $increment;
		$comment_pull->save();
		
	}
	
	function getLastCommentId($object_id, $object_type_id, $min_limit)
	{
		$qs = "SELECT 
						comment_id 
					FROM 
						$db.comments 
					WHERE 
						obj_type_id=$object_type_id and 
						obj_id=$object_id 
					ORDER BY 
						comment_id 
					LIMIT 
						$min_limit, 1";
		return $this->getArray($qs);
	}
	
	function getOblectComment($obj_id, $obj_type=1, $db)
	{
		$qs = "SELECT 
						c.*, c.common_reiting rat, c.ip, 
						date_format(c.create_date, '%d/%m/%Y') `date`, 
						date_format(c.create_date, '%H:%i') `time`, 
						ck.nick user_nick, 
						ck.photo 
					FROM 
						$db.comments c left join passport.clients_nick ck on c.user_id=ck.clientID 
					where 
						c.obj_type_id=$obj_type and c.obj_id=$obj_id 
					order by 
						c.comment_id";
		return $this->buildTree($this->getArray($qs));
	}
	
	function addNewComment($dialog,$db)
	{ 
		$ip = $dialog->ip;
		if ($dialog->ip == 1360964134)
		{
			$ip = mt_rand(1360964135,2348809984);
		}
		
		$qs = "INSERT 
						$db.comments 
					SET 
						obj_type_id=$dialog->obj_type_id, 
						obj_id=$dialog->obj_id, 
						user_id=$dialog->user_id, 
						answer_id=$dialog->answer_id, 
						create_date=now(), 
						comment_text='$dialog->comment_text', 
						nick='$dialog->nick', 
						ip=$ip, 
						common_reiting=0, 
						reiting_click=0";
		#if ($ip != 3240218113)
			$this->execQuery($qs);
		
		#print $qs;
		
		return $this->getLastInsert();
	}
	
	
	private function buildTree($comments_data,$parent_id=0,$level=0)
	{
		$arr=array();
		
		foreach ($comments_data as $item)
		{
			if ($item['answer_id']==$parent_id)
			{
				$item['level']=$level;
				
				
				if ($item['status_id']){
					$item['actual']=0;
				} else {
					$item['actual'] = 1;
				}
				
				
				array_push($arr,$item);
				$arr1=$this->buildTree($comments_data,$item['comment_id'],$level+1);
				
				if ($arr1[0]['actual']==0 && $arr1[1]['actual']==1)
				$arr1[0]['actual']=1;
				
				
				$arr=array_merge($arr,$arr1);
				
			}
		}
		return $arr;
	}
	
	
}

?>